package com.lpgu.security.controller;

import com.lpgu.security.generator.domain.User;
import com.lpgu.security.generator.service.UserService;
import com.lpgu.security.generator.service.impl.UserServiceImpl;
import com.lpgu.security.service.DbUserManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author gulup
 * createDate: 2025/2/12
 * description:
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private DbUserManagerService dbUserManagerService;

    @GetMapping("/user/list")
    public List<User> getList() {
        return userService.list();
    }

    @GetMapping("/user/save")
    public void getList(@RequestParam("username") String userName, @RequestParam("password") String password) {
        List<GrantedAuthority> list = new ArrayList<>();
        //授权，后期需要改为查询数据库动态获得用户拥有的权限和角色
        list.add(new SimpleGrantedAuthority("add"));
        list.add(new SimpleGrantedAuthority("delete"));
        list.add(new SimpleGrantedAuthority("ROLE_ADMIN"));

        dbUserManagerService.createUser(new org.springframework.security.core.userdetails.User(userName,
                password, list));
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

        System.out.println(bCryptPasswordEncoder.encode("123456"));
    }
}
